
package com.jf.cloud.multishop.controller.multishop;

import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.AuditStatus;
import com.jf.cloud.multishop.dto.ShopCompanyDTO;
import com.jf.cloud.multishop.service.CompanyAuditingService;
import com.jf.cloud.multishop.vo.ShopCompanyAuditingVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zz
 * @date 2022/9/13 14:06
 */
@RestController("multishopCompanyAuditingController")
@RequestMapping("/m/shop_company_auditing")
@Tag(name = "multishop-店铺变更工商信息")
public class CompanyAuditingController {

    @Autowired
    private CompanyAuditingService companyAuditingService;

    @PostMapping("/apply_change_company_info")
    @Operation(summary = "申请变更工商信息" , description = "申请变更工商信息")
    public ServerResponseEntity<Void> applyChangeCompanyInfo(@RequestBody @Valid ShopCompanyDTO shopCompanyDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        // 校验是否已经存在申请中记录
        ShopCompanyAuditingVO shopCompanyAuditingVO = companyAuditingService.getLatestAuditingByShopId(shopId);
        if (Objects.nonNull(shopCompanyAuditingVO) && Objects.equals(shopCompanyAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("不能同时提交多次签约申请，请耐心等候平台审核，或者把上次提交申请撤销后重新申请");
        }
        shopCompanyDTO.setShopId(shopId);
        companyAuditingService.applyChangeCompanyInfo(shopCompanyDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/revoke")
    @Operation(summary = "撤销申请" , description = "撤销申请")
    public ServerResponseEntity<Void> revoke() {
        Long shopId = AuthUserContext.get().getTenantId();
        companyAuditingService.revoke(shopId);
        return ServerResponseEntity.success();
    }

}
